package com.example.myhelloworld.client;

import java.util.Vector;

/*
*Two kinds of text problems can be created: a dynamic one with variables that has a numeric answer (represented as a
*string, or a static question/answer-type problem.
*Limitations: There can be a maximum of nine variables (x1 to x9) and the formula is restricted (see Evaluator.java).
*There is as of now no check for proper integer division.
*/
public class TextProblem {
  String problemText;
  String formula;
  Vector<Variable> vars;
  String answer;
  String answerComment;

  Evaluator e = new Evaluator();

  /**
   *When constructed, a text problem first parses the varbounds argument and populates the vars vector.
   *Then the local problemText, formula, answer and comment variables get assigned the strings passed to the
   *constructor but with actual values generated from the variables. Finally, the answer is evaluated.
   *NOTE: There can be a maximum of 9 variables: x1 to x9.
   *@param text A problem statement like "What is x1 plus x2?".
   *@param form A formula to solve the problem, like "x1+x2".
   *@param varbounds A string of hypothetical tuples of (min, max)-values of the variables, like "0 20 0 20".
   *@param comment A comment to the solution, for instance "Just add x1 and x2 together!".
   */
  public TextProblem(String text, String form, String varbounds, String comment) {
    vars = generateVariables(varbounds);
    problemText = varSubst(text, vars);
	//System.out.println("Formula before substitution: "+form);
    formula = varSubst(form, vars);
    //System.out.println("Calling eval on "+formula);
    answer = Integer.toString(e.eval(formula));
    answerComment = varSubst(comment, vars);
  }

  /**
   *Construct a static problem that can have a word or even a sentence as an answer.
   *@param text The problem statement.
   *@param ans The answer.
   *@param comment A comment on the solution.
   */
  public TextProblem(String text, String ans, String comment) {
    problemText = text;
    answer = ans;
    answerComment = comment;
  }

  public String getAnswer() {
    return answer;
  }

  public String getProblemText() {
    return problemText;
  }

  public String getFormula() {
    return formula;
  }

  public String getComment() {
    return answerComment;
  }

  private Vector<Variable> generateVariables(String bounds) {
    Vector<Variable> varvector = new Vector<Variable>();
    String[] splitted = bounds.split(" ");
    for (int i = 1; i<splitted.length; i+=2) {
      varvector.add(new Variable(Integer.parseInt(splitted[i-1]), Integer.parseInt(splitted[i])));
    }
  return varvector;
  }

  private String varSubst(String text, Vector<Variable> vars) {
    String result = "";
    for (int i = 0; i<text.length(); i++) {
      if (Character.toString(text.charAt(i)).equals("x")) {
        int varindex = Integer.parseInt(Character.toString(text.charAt(i+1))) - 1;
        if (varindex == -1) throw new IndexOutOfBoundsException("An x0 was detected in a text problem, should be x1.");
        result += Integer.toString(vars.elementAt(varindex).getValue());
        i += 2;
        if (i>=text.length()) break;
      }
      result += Character.toString(text.charAt(i));
    }
    return result;
  }

  //Main test routine:
  public static void main(String[] args) {
    TextProblem tp = new TextProblem("What is x1 + x2?", "x1 + x2", "1 20 1 20", "Simply add x1 and x2 together.");
    System.out.println(tp.getProblemText());
    System.out.println(tp.getFormula());
    System.out.println(tp.getAnswer());
    System.out.println(tp.getComment());

    TextProblem tp2 = new TextProblem("Who are you?", "I am me", "This is a deep philosophical question!");
    System.out.println(tp2.getProblemText());
    System.out.println(tp2.getAnswer());
    System.out.println(tp2.getComment());
  }
}